package com.example.notepadtxx;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.notepadtxx.adapter.NotePadAdapter;
import com.example.notepadtxx.bean.NotepadBean;
import com.example.notepadtxx.database.MyHelper;

import java.util.List;

public class NotePadActivity extends AppCompatActivity {
    private ImageView add;
    private MyHelper myHelper;
    private List<NotepadBean> list;//存储查询出来的数据
    private ListView listView; //用于显示查询出来的数据
    private NotePadAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notepad);
        add = (ImageView)findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(NotePadActivity.this,RecordActivity.class);
                startActivityForResult(intent,1);//数据回传
            }
        });
        listView = (ListView)findViewById(R.id.listview);//用于显示查询出的记录的列表
        initData();
    }
    protected void initData(){
        myHelper = new MyHelper(this);//创建数据库
        showQueryData();//数据库查询
        //给item添加点击事件监听，完成修改交互
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(NotePadActivity.this,RecordActivity.class);
                NotepadBean notepadBean = list.get(i);
                intent.putExtra("id",notepadBean.getId());
                intent.putExtra("content",notepadBean.getNotepadContent());
                intent.putExtra("notetime",notepadBean.getNotepadTime());
                //带值跳转到修改界面
                startActivityForResult(intent,1);
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                final AlertDialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder(NotePadActivity.this)
                        .setMessage("是否删除此记录？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                NotepadBean notepadBean = list.get(i);
                                if(myHelper.deleteData(notepadBean.getId())){
                                    list.remove(i);
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(NotePadActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                                }
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //dialog.dismiss();//关闭对话框
                    }
                });
                dialog = builder.create();
                dialog.show();
                return true;
            }
        });
    }
    private void showQueryData(){
        if(list !=null){
            list.clear();
        }
        //从数据库中查询数据
        list = myHelper.queryData();
        adapter = new NotePadAdapter(this,list);//定义适配器
        listView.setAdapter(adapter);//绑定适配器
    }
    //返回该界面时会自动调用onActivityResult方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //更新界面
        if(requestCode==1 && resultCode==2){
            showQueryData();
        }
    }
}
